package com.aoxin.service.impl.course;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aoxin.mapper.course.SlfSchoolCourseDtoMapper;
import com.aoxin.mapper.course.systemCourse.SchoolCoursePageDao;
import com.aoxin.mapper.course.webCourse.MyCourseMapper;
import com.aoxin.mapper.course.webCourse.SlfChildrenMapper;
import com.aoxin.mapper.opm.SlfSchoolMapper;
import com.aoxin.model.course.SchoolCoursePage;
import com.aoxin.model.dto.SchoolCourseDto;
import com.aoxin.model.opm.SlfSchool;
import com.aoxin.service.inter.course.SchoolCourseDtoService;
import com.aoxin.util.ThreadLocalUtil;

@Service
public class SchoolCourseDtoServiceImpl implements SchoolCourseDtoService {

    @Autowired
    SlfSchoolCourseDtoMapper slfSchoolCourseDtoMapper;
    @Autowired
    private SchoolCoursePageDao schoolCoursePageDao;
    @Autowired
    SlfSchoolMapper slfSchoolMapper;
    @Autowired
    private MyCourseMapper myCourseMapper;
    @Autowired
    private SlfChildrenMapper slfChildrenMapper;

    @Override
    public List<SchoolCourseDto> getSchoolCourseDtoList(Integer schoolId,Integer integer) {
        List<SchoolCourseDto> schoolCourseDtoList = new ArrayList<SchoolCourseDto>();
        if(schoolId != null){
            schoolCourseDtoList = slfSchoolCourseDtoMapper.getSchoolCourseDtoList(schoolId,integer);
            for(SchoolCourseDto schoolCourseDto : schoolCourseDtoList){
                Integer coursePageId = schoolCourseDto.getId();
                SchoolCoursePage schoolCoursePage = new SchoolCoursePage();
                schoolCoursePage.setSchoolCoursePageId(coursePageId);
                
                SlfSchool  slfschool= slfSchoolMapper.selectByPrimaryKey(schoolId);
                schoolCourseDto.setSchoolName(slfschool.getName());
                List<SchoolCoursePage> schoolCoursePageList =  schoolCoursePageDao.getSchoolCoursePage(schoolCoursePage);
                Integer parentId = schoolCoursePageList.get(0).getParentId();
                schoolCoursePage.setParentId(parentId);
                List<SchoolCoursePage> schoolCoursePages =  schoolCoursePageDao.getSchoolCoursePage(schoolCoursePage);
                Integer childrenId = ThreadLocalUtil.getChildren().getChildrenId();
                int a=0;
                if(schoolCoursePages.size()>0) {
                	a = this.myCourseMapper.getCountMyCourse(childrenId, schoolCoursePages.get(0).getSchoolCoursePageId());	
                }else {
                	break;
                }
                if(a == 1){
                    schoolCourseDto.setIsUpBuy("Y");
                }
                int count = this.myCourseMapper.getCountMyCourse(childrenId, schoolCourseDto.getId());
                if(count == 1){
                    schoolCourseDto.setIsBuy("Y");
                }
            }
        }
        return schoolCourseDtoList;
    }

	@Override
	public SchoolCourseDto getSchoolCourseDto(Integer schoolId, Integer courseId) {
		return this.slfSchoolCourseDtoMapper.getSchoolCourse(schoolId, courseId);
	}
}
